<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-loading="loading">
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="append"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table :data="list.rows">
              <el-table-column label="序号" width="100" type="index" :index="indexMethod" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="RoleDetails(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :page-sizes="[3, 5, 10, 15]"
              :page-size="100"
              layout="total, sizes, prev, pager, next, jumper"
              :total="list.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />

          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <addDep :id="id" ref="addDep" :show-dialog.sync="showDialog" />
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import addDep from './components/add-dept.vue'
import { getRoleListApi, DestroyCharacter } from '@/api/setting'
import { getCompanyByIdApi } from '@/api/company'
export default {
  name: 'Dashboard',
  components: {
    addDep
  },
  data() {
    return {
      list: [],
      page: 1,
      pagesize: 3,
      loading: false,
      showDialog: false,
      id: '',
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      }
    }
  },
  computed: {
    ...mapState('user', ['setUser'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 列表序号问题
    indexMethod(index) {
      return (this, this.page - 1) * this.pagesize + index + 1
    },
    // 获取数据
    async getRoleList() {
      this.loading = true
      const { data: res } = await getRoleListApi(this.page, this.pagesize)
      this.list = res
      // console.log(res)
      this.loading = false
    },
    // 绑定分页页码
    handleCurrentChange(val) {
      this.page = val
      this.getRoleList()
    },
    // 绑定分页单页个数
    handleSizeChange(val) {
      this.pagesize = val
      this.getRoleList()
    },
    // 删除
    async handleDelete(id) {
      this.$confirm('是否删除该角色？', '提示').then(async() => {
        const res = await DestroyCharacter(id)
        // 判断list的下标等于1或者页数大一
        if (this.list.length === 1 && this.page > 1) {
          // 页数减一
          this.page--
        }
        // console.log(res)
        // 重新拉取数据
        this.getRoleList()
        // 弹窗提示
        this.$message.success(res.message)
      }).catch(() => {})
    },
    // 新增
    append() {
      this.showDialog = true
    },
    // 编辑
    RoleDetails(id) {
      this.id = id
      this.$nextTick(() => {
        this.$refs.addDep.RoleDetail()
      })
      this.showDialog = true
    },
    // 获取公司列表
    async getCompanyInfo() {
      const { data } = await getCompanyByIdApi(this.setUser.companyId)
      console.log(data)
      this.companyForm = data
    }
  }
}
</script>

<style>

</style>
